﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using MLY;
using MLY.Bll;
using Microsoft.Office.Interop.Excel;

namespace MLY.LTR
{
    public partial class Login : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserId"] != null)
                Response.RedirectTo("/LTR/default.aspx", true);
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            Bll.clsUser user = new clsUser(this.txtUsername.Text);
            if (!user.CheckPassword(txtPassword.Text))
            {
                string errorMessage = Dictionary.Translate("Login.FaildMessage");
                // שם משתמש או סיסמה לא מוכרים במערכת
                Response.Write("<script language='javascript'>alert('" + errorMessage + "');</script>");

            }
            else
            {
                Utils.currUserId = user.Data.UserId;
                Session["FullName"] = user.Data.FullName;
                Bll.clsOrganization org = new clsOrganization((int)user.Data.OrganiztionId);
                if (org.IsExist)
                    Session["CompanyName"] = org.Data.OrganizationName;
                SessParam.Culture = user.Data.Culture;
                Session["RoleId"] = user.Data.RollId;
                Session["OrganizationId"] = user.Data.OrganiztionId;
                Session["DefaultLinesinGrid"] = user.Data.DefaultLinesinGrid;
                Session["IsClient"] = Bll.clsOrganization.IsClient(user.Data.OrganiztionId.HasValue ? user.Data.OrganiztionId.Value : 0  );
                string strGoto = Request.QueryString["ReturnTo"];
                LogUserLogin(Utils.currUserId);

                // DEMO CODE
                Demo.Login(Session["UserId"].ToString());
                
                // END OF DEMO CODE
                
                if ((string.IsNullOrEmpty(strGoto)) || (strGoto.IndexOf("/Login.aspx", StringComparison.CurrentCultureIgnoreCase) >= 0))
                {
                    Response.RedirectTo("~/LTR/Default.aspx");
                }
                else
                {
                    Response.RedirectTo(strGoto);
                }

                
            }
        }

        private void LogUserLogin(int UserId)
        {
            using (Dal.lqKbmDataContext dc = new MLY.Dal.lqKbmDataContext())
            {
                MLY.Dal.TableUserLogin login = new MLY.Dal.TableUserLogin();

                login.IPAddress = Utils.GetIP();
                login.UserId = UserId;

                dc.TableUserLogins.InsertOnSubmit(login);
                dc.SubmitChanges();
            }

        }

        //*********
        // http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/6264b659-f366-4c29-817b-d51b738212f5/
        // microsoft.office.interop.excel - start reading there.
        //protected void ExcelLoad(object sender, EventArgs e)
        //{ 
        //    Application app = new Microsoft.Office.Interop.Excel.Application();

        //    Workbook wb = app.Workbooks.Open("c:\\\\_pastch_\\Xalturot\\MLY\\hours_Shaul.xlsx", Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        //        Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing, Type.Missing);

        //    Worksheet sheet = (Worksheet)wb.Sheets["August 2011"];

        //    Range excelRange = sheet.UsedRange;
        //    string result = "";
        //    Range workingRangeCells = sheet.get_Range("A0:A1", Type.Missing);
        //    System.Array array = (System.Array)workingRangeCells.Cells.Value2;
        //    string A1 = array.GetValue(0).ToString();

        //    //foreach (Range row in excelRange.Rows)
        //    //{
        //    //    int RowNumber = row.Row;

        //    //    Range workingRangeCells = sheet.get_Range("A0:A1", Type.Missing);

        //    //    System.Array array = (System.Array)workingRangeCells.Cells.Value2;

        //    //    string A1 = array.GetValue(0).ToString();

        //    //    result += A1 + "<br /> ";

        //    //}
        //    TempResultLabel.Text = result;
        //}

        

    }
}
